package com.epalmpay.controller.channel.activity;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.activity.*;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.activity.IOtherCouponService;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by gwn on 2017/9/25.
 */
@Controller
@RequestMapping(value="/channel/activity")
public class OtherCouponController extends BaseController{

    @Resource
    private IOtherCouponService otherCouponService;

    /**
     * 显示优惠券列表
     *
     */
    @RequestMapping(value="/otherCouponList")
    @ResponseBody
    public OtherCouponListOutDTO  otherCouponList( OtherCouponInDTO couponInDTO,PageDTO pageDTO){
        OtherCouponListOutDTO couponListOutDTO=new OtherCouponListOutDTO();

        try {
            Page page= Tools.pageEmtity(pageDTO);

            //查询时取数据
            if(couponInDTO.getIsReload()){
                couponListOutDTO = otherCouponService.getOtherCouponList(couponInDTO,page);
            }
            couponListOutDTO.setPage(page);
            couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return couponListOutDTO;
     } catch (ApplicationException ae) {
            couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return couponListOutDTO;
    }

    /**
     * 显示优惠券列表
     *
     */
    @RequestMapping(value="/otherCouponExporList")
    @ResponseBody
    public OtherCouponExporListOutDTO  otherCouponExporList( OtherCouponInDTO couponInDTO){
    	OtherCouponExporListOutDTO couponExporListOutDTO=new OtherCouponExporListOutDTO();

        try {
         
            //查询时取数据
        	couponExporListOutDTO = otherCouponService.getOtherCouponExporList(couponInDTO);
            
        	couponExporListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return couponExporListOutDTO;
     } catch (ApplicationException ae) {
    	 couponExporListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
        	couponExporListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return couponExporListOutDTO;
    }


    

    /**
     * 保存优惠卷
     * @param couponAddInDTO
     * @return
     */
    @RequestMapping(value="/otherCouponSave")
    @ResponseBody
    public BaseOutDTO  otherCouponSave(@RequestBody CouponAddInDTO couponAddInDTO){
    	BaseOutDTO couponListOutDTO=new BaseOutDTO();
        try {
        	Long recordId=otherCouponService.otherCouponSave(couponAddInDTO);
            couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
//            String content ="新增优惠券";
//            sysLogService.addSysLog(couponAddInDTO.getUserId(),couponAddInDTO.getUserName(),null,content,couponAddInDTO.getGroupId());
            otherCouponService.saveSynCreateCodeInfo(recordId);
            return couponListOutDTO;
        } catch (ApplicationException ae) {
            couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
//            logger.error(ae);
        }
        catch (Exception e) {
            couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
//            logger.error(e);
        }
        return couponListOutDTO;
    }


    @RequestMapping(value="/otherCouponDetail")
    @ResponseBody
    public CouponDetailOutDTO  otherCouponDetail(OtherCouponInDTO couponInDTO,PageDTO pageDTO){
        CouponDetailOutDTO result = new CouponDetailOutDTO();
        try {
            Page page=Tools.pageEmtity(pageDTO);
            result = otherCouponService.getOtherCouponDetail(couponInDTO,page);
            result.setPage(page);
            result.setErrorCode(Rescode.SUCCESS, "处理成功");
            return result;
        } catch (ApplicationException ae) {
            result.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return result;
    }



    @RequestMapping(value="/otherCouponUpdate")
    @ResponseBody
    public OtherCouponOutDTO otherCouponUpdate(OtherCouponInDTO otherCouponInDTO){
        OtherCouponOutDTO otherCouponOutDTO=new OtherCouponOutDTO();
        try {
            otherCouponService.updateOtherCoupon(otherCouponInDTO);
            otherCouponOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
            return otherCouponOutDTO;
        }  catch (ApplicationException ae) {
            otherCouponOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            otherCouponOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return otherCouponOutDTO;
    }

    @RequestMapping(value="/otherCouponImport")
    @ResponseBody
    public OtherCouponOutDTO otherCouponImport(@RequestBody OtherCouponImportInDTO otherCouponImportInDTO){
        OtherCouponOutDTO otherCouponOutDTO=new OtherCouponOutDTO();
        try {
            otherCouponService.finihsOtherCoupon(otherCouponImportInDTO);
            otherCouponOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
            return otherCouponOutDTO;
        }  catch (ApplicationException ae) {
            otherCouponOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            otherCouponOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return otherCouponOutDTO;
    }
    
    /**otherCoupon 申请互发申请相关接口**/
    @RequestMapping(value="/otherCouponApply")
    @ResponseBody
    public BaseOutDTO otherCouponApply(OtherCouponComfireInDTO otherCouponInDTO){
    	BaseOutDTO baseOutDTO=new BaseOutDTO();
        try {
            otherCouponService.finishOCouponApply(otherCouponInDTO);
            baseOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
            return baseOutDTO;
        }  catch (ApplicationException ae) {
        	baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
        	baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return baseOutDTO;
    }
    
    @RequestMapping(value="/otherCouponComfireDelete")
    @ResponseBody
    public BaseOutDTO otherCouponComfireDelete(OtherCouponComfireInDTO otherCouponInDTO){
    	BaseOutDTO baseOutDTO=new BaseOutDTO();
        try {
            otherCouponService.finishOCouponDelete(otherCouponInDTO);
            baseOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
            return baseOutDTO;
        }  catch (ApplicationException ae) {
        	baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
        	baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return baseOutDTO;
    }
    
    
    @RequestMapping(value="/otherCouponComfire")
    @ResponseBody
    public BaseOutDTO otherCouponComfire(OtherCouponComfireInDTO otherCouponInDTO){
    	BaseOutDTO baseOutDTO=new BaseOutDTO();
        try {
            otherCouponService.finishOCouponComfire(otherCouponInDTO);
            baseOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
            return baseOutDTO;
        }  catch (ApplicationException ae) {
        	baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
        	baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return baseOutDTO;
    }
    @RequestMapping(value="/otherCouponComfireList")
    @ResponseBody
    public OCouponComfireListOutDTO  otherCouponComfireList( OtherCouponComfireInDTO couponInDTO,PageDTO pageDTO){
    	OCouponComfireListOutDTO couponListOutDTO=new OCouponComfireListOutDTO();

        try {
            Page page= Tools.pageEmtity(pageDTO);

            //查询时取数据
            couponListOutDTO = otherCouponService.otherCouponComfireList(couponInDTO,page);
            couponListOutDTO.setPage(page);
            couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return couponListOutDTO;
     } catch (ApplicationException ae) {
            couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return couponListOutDTO;
    }

 
    @RequestMapping(value="/otherCouponComfireInfo")
    @ResponseBody
    public OCouponComfireInfoOutDTO  otherCouponComfireInfo(OtherCouponComfireInDTO couponInDTO){
    	OCouponComfireInfoOutDTO result = new OCouponComfireInfoOutDTO();
        try {
           
            result = otherCouponService.otherCouponComfireInfo(couponInDTO.getId(),couponInDTO.getProcessStatus());
           
            result.setErrorCode(Rescode.SUCCESS, "处理成功");
            return result;
        } catch (ApplicationException ae) {
            result.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return result;
    }

    
}
